package com.example.bingobox.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.bingobox.R;
import com.google.android.material.chip.Chip;
import com.google.android.material.chip.ChipGroup;
import java.util.*;

public class SceneGroupDisplayAdapter extends RecyclerView.Adapter<SceneGroupDisplayAdapter.ViewHolder> {
    private final Map<String, List<String>> sceneGroups = new LinkedHashMap<>();
    private final Set<String> selectedScenes;
    private final OnSceneSelectedListener listener;

    public interface OnSceneSelectedListener {
        void onSceneSelected(String scene, boolean isSelected);
    }

    public SceneGroupDisplayAdapter(OnSceneSelectedListener listener, Set<String> selectedScenes) {
        this.listener = listener;
        this.selectedScenes = selectedScenes;
    }

    public void setScenes(Collection<String> scenes) {
        sceneGroups.clear();
        for (String scene : scenes) {
            String[] parts = scene.split("-", 2);
            if (parts.length == 2) {
                sceneGroups.computeIfAbsent(parts[0], k -> new ArrayList<>()).add(parts[1]);
            }
        }
        // 对每个大场景下的小场景进行排序
        for (List<String> minorScenes : sceneGroups.values()) {
            Collections.sort(minorScenes);
        }
        notifyDataSetChanged();
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_scene_group_display, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        String majorScene = new ArrayList<>(sceneGroups.keySet()).get(position);
        List<String> minorScenes = sceneGroups.get(majorScene);
        
        holder.majorSceneText.setText(majorScene + "：");
        holder.minorScenesGroup.removeAllViews();
        
        Context context = holder.itemView.getContext();
        if (minorScenes != null) {
            for (String minorScene : minorScenes) {
                Chip chip = new Chip(context);
                chip.setText(minorScene);
                chip.setCheckable(true);
                String fullScene = majorScene + "-" + minorScene;
                chip.setChecked(selectedScenes.contains(fullScene));
                
                chip.setOnCheckedChangeListener((buttonView, isChecked) -> {
                    if (listener != null) {
                        listener.onSceneSelected(fullScene, isChecked);
                    }
                });
                
                holder.minorScenesGroup.addView(chip);
            }
        }
    }

    @Override
    public int getItemCount() {
        return sceneGroups.size();
    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView majorSceneText;
        ChipGroup minorScenesGroup;

        ViewHolder(View itemView) {
            super(itemView);
            majorSceneText = itemView.findViewById(R.id.text_major_scene);
            minorScenesGroup = itemView.findViewById(R.id.chip_group_minor_scenes);
        }
    }
} 